package com.ruoyi.system.service.impl;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.labdata.domain.LabFile;
import com.ruoyi.labdata.service.ILabFileService;
import com.ruoyi.labdata.service.ILabFlowInstanceService;
import com.ruoyi.system.domain.*;
import com.ruoyi.system.mapper.ProjectCreationMapper;
import com.ruoyi.system.service.*;
import com.ruoyi.system.utils.FileTypeEnum;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 项目创建申请Service业务层处理
 *
 * @author ruoyi
 * @date 2022-02-13
 */
@Service
public class ProjectCreationServiceImpl implements IProjectCreationService {

    private static final String belongType = FileTypeEnum.Project.getValue();;

    @Resource
    private ProjectCreationMapper projectCreationMapper;

    @Autowired
    private ILabFileService labFileService;

    @Autowired
    private ISysUserService sysUserService;

    @Autowired
    private ILabFlowInstanceService labFlowInstanceService;

    @Autowired
    private ILabProjectInstrumentService labProjectInstrumentService;

    @Autowired
    private ILabProjectWorkResearchService labProjectWorkResearchService;

    @Autowired
    private ILabProjectConstructionService labProjectConstructionService;

    @Autowired
    private ILabProjectTeachingCultivationService labProjectTeachingCultivationService;

    @Autowired
    private ILabProjectDevelopmentProjectService labProjectDevelopmentProjectService;


    /**todo
     * 查询项目创建申请
     *
     * @param instancetId 项目创建申请ID
     * @return 项目创建申请
     */
    @Override
    public ProjectCreation selectProjectCreationById(Long instancetId) {
        List<LabFile> labFiles = labFileService.getFiles(instancetId, belongType);
        ProjectCreation projectCreation = projectCreationMapper.selectProjectCreationById(instancetId);
        projectCreation.setImages(labFiles);
        // 查询实验室仪器
        if(1==projectCreation.getLabProjectOpt()){
            projectCreation.setLabProjectInstrument(labProjectInstrumentService.selectLabProjectInstrumentById(instancetId));
        }
        // 查询工作研究
        if(2==projectCreation.getLabProjectOpt()){
            projectCreation.setLabProjectWorkResearch(labProjectWorkResearchService.selectLabProjectWorkResearchById(instancetId));
        }
        // 查询建设项目
        if(3==projectCreation.getLabProjectOpt()){
            projectCreation.setLabProjectConstruction(labProjectConstructionService.selectLabProjectConstructionById(instancetId));
        }
        // 教学培育
        if(4==projectCreation.getLabProjectOpt()){
            projectCreation.setLabProjectTeachingCultivation(labProjectTeachingCultivationService.selectLabProjectTeachingCultivationById(instancetId));
        }

        // 开发项目
        if(5==projectCreation.getLabProjectOpt()){
            projectCreation.setLabProjectDevelopmentProject(labProjectDevelopmentProjectService.selectLabProjectDevelopmentProjectById(instancetId));
        }
        return projectCreation;
    }

    /**
     * 查询项目创建申请列表
     *
     * @param projectCreation 项目创建申请
     * @return 项目创建申请
     */
    @Override
    public List<ProjectCreation> selectProjectCreationList(ProjectCreation projectCreation) {
        SysUser loginUser = SecurityUtils.getLoginUser().getUser();
        projectCreation.setCreateBy(loginUser.getUserName());
        List<ProjectCreation> projectCreations = projectCreationMapper.selectProjectCreationList(projectCreation);
        return projectCreations;
    }

    /**
     * 新增项目创建申请
     *
     * @param projectCreation 项目创建申请
     * @return 结果
     */
    @Override
    @Transactional
    public int insertProjectCreation(ProjectCreation projectCreation) {

        SysUser user = sysUserService.selectUserByUserName(projectCreation.getCreateBy());
        projectCreation.setCreateTime(DateUtils.getNowDate());
        projectCreation.setCreateUserId(user.getUserId());
        projectCreation.setCreateOrgId(user.getDeptId());
        projectCreation.setCreateTime(new Date());
        labFlowInstanceService.insertLabFlowInstance(projectCreation);
        labFileService.insertFiles(projectCreation.getImages(), projectCreation.getInstanceId(), belongType);
        // 保存实验仪器
        if (1==projectCreation.getLabProjectOpt()){
            LabProjectInstrument labProjectInstrument = projectCreation.getLabProjectInstrument();
                labProjectInstrument.setInstanceId(projectCreation.getInstanceId());
                labProjectInstrumentService.insertLabProjectInstrument(labProjectInstrument);
        }

        // 保存工作研究
        if (2==projectCreation.getLabProjectOpt()){
            LabProjectWorkResearch labProjectWorkResearch = projectCreation.getLabProjectWorkResearch();
            labProjectWorkResearch.setInstanceId(projectCreation.getInstanceId());
            labProjectWorkResearchService.insertLabProjectWorkResearch(labProjectWorkResearch);
        }

        // 保存建设项目
        if (3==projectCreation.getLabProjectOpt()){
            LabProjectConstruction labProjectConstruction = projectCreation.getLabProjectConstruction();
            labProjectConstruction.setInstanceId(projectCreation.getInstanceId());
            labProjectConstructionService.insertLabProjectConstruction(labProjectConstruction);
        }

        // 保存教学培育
        if (4==projectCreation.getLabProjectOpt()){
            LabProjectTeachingCultivation labProjectTeachingCultivation = projectCreation.getLabProjectTeachingCultivation();
            labProjectTeachingCultivation.setInstanceId(projectCreation.getInstanceId());
            labProjectTeachingCultivationService.insertLabProjectTeachingCultivation(labProjectTeachingCultivation);
        }


        // 开发项目
        if (5==projectCreation.getLabProjectOpt()){
            LabProjectDevelopmentProject labProjectDevelopmentProject = projectCreation.getLabProjectDevelopmentProject();
            labProjectDevelopmentProject.setInstanceId(projectCreation.getInstanceId());
            labProjectDevelopmentProjectService.insertLabProjectDevelopmentProject(labProjectDevelopmentProject);
        }

        return projectCreationMapper.insertProjectCreation(projectCreation);
    }

    /**
     * 修改项目创建申请
     *
     * @param projectCreation 项目创建申请
     * @return 结果
     */
    @Override
    @Transactional
    public int updateProjectCreation(ProjectCreation projectCreation) {
        SysUser user = sysUserService.selectUserByUserName(projectCreation.getCreateBy());
        projectCreation.setCreateOrgId(user.getDeptId());
        labFlowInstanceService.updateLabFlowInstance(projectCreation);
        labFileService.updateFiles(projectCreation.getImages(), projectCreation.getInstanceId(), belongType);
        //------------ 先删除再新增
        // 删除实验仪器
        labProjectInstrumentService.deleteLabProjectInstrumentById(projectCreation.getInstanceId());
        // 删除工作研究
        labProjectWorkResearchService.deleteLabProjectWorkResearchById(projectCreation.getInstanceId());
        // 删除建设项目
        labProjectConstructionService.deleteLabProjectConstructionById(projectCreation.getInstanceId());
        // 删除教学培育
        labProjectTeachingCultivationService.deleteLabProjectTeachingCultivationById(projectCreation.getInstanceId());
        // 删除开发项目
        labProjectDevelopmentProjectService.deleteLabProjectDevelopmentProjectById(projectCreation.getInstanceId());



        // 更新实验仪器
        if (1==projectCreation.getLabProjectOpt()){
            LabProjectInstrument labProjectInstrument = projectCreation.getLabProjectInstrument();
            labProjectInstrument.setInstanceId(projectCreation.getInstanceId());
            labProjectInstrumentService.insertLabProjectInstrument(labProjectInstrument);
        }

        // 更新工作研究
        if (2==projectCreation.getLabProjectOpt()){
            LabProjectWorkResearch labProjectWorkResearch = projectCreation.getLabProjectWorkResearch();
            labProjectWorkResearch.setInstanceId(projectCreation.getInstanceId());
            labProjectWorkResearchService.insertLabProjectWorkResearch(labProjectWorkResearch);
        }

        // 新增建设项目
        if (3==projectCreation.getLabProjectOpt()){
            LabProjectConstruction labProjectConstruction = projectCreation.getLabProjectConstruction();
            labProjectConstruction.setInstanceId(projectCreation.getInstanceId());
            labProjectConstructionService.insertLabProjectConstruction(labProjectConstruction);
        }

        // 保存教学培育
        if (4==projectCreation.getLabProjectOpt()){
            LabProjectTeachingCultivation labProjectTeachingCultivation = projectCreation.getLabProjectTeachingCultivation();
            labProjectTeachingCultivation.setInstanceId(projectCreation.getInstanceId());
            labProjectTeachingCultivationService.insertLabProjectTeachingCultivation(labProjectTeachingCultivation);
        }

        // 开发项目
        if (5==projectCreation.getLabProjectOpt()){
            LabProjectDevelopmentProject labProjectDevelopmentProject = projectCreation.getLabProjectDevelopmentProject();
            labProjectDevelopmentProject.setInstanceId(projectCreation.getInstanceId());
            labProjectDevelopmentProjectService.insertLabProjectDevelopmentProject(labProjectDevelopmentProject);
        }
        return projectCreationMapper.updateProjectCreation(projectCreation);
    }

    @Override
    public int updateProjectCreationStatus(ProjectCreation projectCreation) {
        return projectCreationMapper.updateProjectCreation(projectCreation);
    }

    /**
     * 批量删除项目创建申请
     *
     * @param instancetIds 需要删除的项目创建申请ID
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteProjectCreationByIds(Long[] instancetIds) {
        for (Long instrumentationId : instancetIds) {
            labFileService.deleteLabFile(instrumentationId, belongType);
        }
        // 删除流程
        labFlowInstanceService.deleteLabFlowInstanceByIds(instancetIds);
        // 删除实验仪器
        labProjectInstrumentService.deleteLabProjectInstrumentByIds(instancetIds);
        // 删除工作研究
        labProjectWorkResearchService.deleteLabProjectWorkResearchByIds(instancetIds);
        // 删除建设项目
        labProjectConstructionService.deleteLabProjectConstructionByIds(instancetIds);
        // 删除教学培育
        labProjectTeachingCultivationService.deleteLabProjectTeachingCultivationByIds(instancetIds);
        // 删除开发项目
        labProjectDevelopmentProjectService.deleteLabProjectDevelopmentProjectByIds(instancetIds);

        return projectCreationMapper.deleteProjectCreationByIds(instancetIds);
    }

    /**
     * 删除项目创建申请信息
     *
     * @param projectFlowId 项目创建申请ID
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteProjectCreationById(Long projectFlowId) {
        labFileService.deleteLabFile(projectFlowId, belongType);
        return projectCreationMapper.deleteProjectCreationById(projectFlowId);
    }

    @Override
    @Transactional
    public int commitProjectCreation(ProjectCreation projectCreation) {
        this.updateProjectCreation(projectCreation);
        return labFlowInstanceService.commitFlow(projectCreation);
    }

    @Override
    public List<LabFile> getInstanceFiles(Long instanceId) {
        return labFileService.getFiles(instanceId, belongType);
    }


}
